Report post

How do I execute a JavaScript immediately after a page is loaded?

Execute a JavaScript immediately after a page has been loaded: More "Try it Yourself" examples below. The onload event occurs when an object has been loaded. onload is most often used within the element to execute a script once a web page has completely loaded all content (including images, script files, CSS files, etc.).

What is the difference between body onload and window onload?

With , document.body.onload might be null, undefined or a function depending on the browser (although getAttribute ("onload") should be somewhat consistent for getting the body of the anonymous function as a string). With window.onload, when you assign a function to it, window.onload will be a function consistently across browsers.

How does the onload attribute work?

The onload attribute works the same way, except that it takes a function as its value instead of a string like the value attribute does. That also explains why you can "only use one of them" - calling window.onload reassigns the value of the onload attribute for the body tag.

Should I use body onload>?

There's not much reason to use ; anyway when you can use window.onload. In Opera, the event target for window.onload and (and even window.addEventListener ("load", func, false)) will be the window instead of the document like in Safari and Firefox. But, 'this' will be the window across browsers.

Related articles

The World's Leading Crypto Trading Platform

Get my welcome gifts